package JDBC_Test.park.Service;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;

import JDBC_Test.park.Dao.RecordDao;
import JDBC_Test.park.Dao.ParkingLotDao;
import JDBC_Test.park.Model.ParkRecordModel;
import JDBC_Test.park.Model.ParkingLotModel;

//import org.junit.jupiter.api.Test;

public class ParkService {

	RecordDao recordDao = new RecordDao();
	ParkingLotDao parkingLotDao = new ParkingLotDao();

	public static void main(String[] args) throws SQLException {
		ParkService parkService = new ParkService();
		parkService.print();

//		parkService.parkingLotDao.insert(100);
		Scanner scanner = new Scanner(System.in);

		while (true) {
			System.out.println("请输入操作：");
			String string = scanner.nextLine();
			int operation = Integer.valueOf(string);
			if (operation == 0) {
				System.out.println("感谢使用，再见");
				break;
			}
			switch (operation) {
			case 1:
				parkService.parkCar();
				break;
			case 2:
				parkService.returnCar();
				break;
			case 3:
				parkService.selectState();
				break;
			case 4:
				parkService.selectRecord();
				break;
			default:
				break;
			}
		}

//		scanner.close();
	}
	/**
	 * 查询停车记录
	 * @throws SQLException
	 */
	private void selectRecord() throws SQLException {
		// TODO Auto-generated method stub
		recordDao.selectAll();
	}
	/**
	 * 查询车位状态
	 * @throws SQLException 
	 */
	private void selectState() throws SQLException {
		// TODO Auto-generated method stub
		parkingLotDao.selectAll();
	}

	/**
	 * 取车
	 * 
	 * @throws SQLException
	 */
	private void returnCar() throws SQLException {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("输入车牌号：");
		String license = scanner.nextLine();

		// 先查出该车停车时的时间
		ParkRecordModel parkRecordModel = new ParkRecordModel(null, license, null, null, 0.0);
		Timestamp t = recordDao.selectByCarForTime(parkRecordModel);
		long start = t.getTime() / 1000;
		Date endTime = new Date();// 当前时间
		long end = endTime.getTime() / 1000;
		Double money = (double) ((end - start) * 2);// 计算停车费用

		Timestamp end_time = new java.sql.Timestamp(endTime.getTime());
		// 查询该车所停的车位
		Integer res = recordDao.selectByCarForParkId(parkRecordModel);

		// 更新车位状态
		ParkingLotModel parkingLotModel = new ParkingLotModel(res, 0);
		parkingLotDao.updateState(parkingLotModel);

		// 更新停车记录
		ParkRecordModel parkRecordModel1 = new ParkRecordModel(res, license, t, end_time, money);
		recordDao.update(parkRecordModel1);
		System.out.println("成功！");
	}

	/**
	 * 停车
	 * 
	 * @throws SQLException
	 */
	private void parkCar() throws SQLException {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("输入车位号：");
		String s0 = scanner.nextLine();
		Integer parkNo = Integer.valueOf(s0);
		ParkingLotModel parkingLotModel = new ParkingLotModel(parkNo, 1);
		// 校验车位的合理性
		boolean flag = parkingLotDao.selectState(parkingLotModel);
		if (flag == false) {
			System.out.println("当前车位已被占用");
			return;
		}
		System.out.println("输入车牌号：");
		String license = scanner.nextLine();
		System.out.println("输入车高：");
		String s = scanner.nextLine();
		Double height = Double.parseDouble(s);
		Date startDate = new Date();
		if (height > 3) {
			System.out.println("不符合停车要求（3m限高）");
			return;
		}
		ParkRecordModel parkRecordModel = new ParkRecordModel(parkNo, license, startDate, null, 0.0);
		recordDao.insert(parkRecordModel);// 插入记录
		parkingLotDao.updateState(parkingLotModel);// 更新车位状态

		System.out.println("成功！");
	}

	public void print() {
		System.out.println("--------停车场管理 ---------");
		System.out.println("--------1:停车      ---------");
		System.out.println("--------2:取车      ---------");
		System.out.println("--------3:查询当前所有车位状态-");
		System.out.println("--------4:查询所有停车收费记录--");
		System.out.println("--------0:退出      ---------");
	}

}
